package com.jt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.ItemCatMapper;
import com.jt.mapper.ItemMapper;
import com.jt.pojo.Item;
import com.jt.service.ItemService;
import com.jt.vo.EasyUITable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {
	
	@Autowired
	private ItemMapper itemMapper;

	/**
	 * 分页sql
	 * 	第一页：select * from tb_item limit 0,20;
	 * 	第二页：select * from tb_item limit 20,20;
	 * 	第三页：select * from tb_item limit 40,20;
	 *
	 * @param page
	 * @param rows
	 * @return
	 */
	@Override
	public EasyUITable findItemByPage(int page, int rows) {
		/* long total=itemMapper.selectCount(null);
		//1、手写分页
		int startIndex=(page-1)*rows;
		List<Item> itemList=itemMapper.findItemByPage(startIndex,rows);*/

		IPage mpPage=new Page(page,rows);
		QueryWrapper<Item> queryWrapper=new QueryWrapper<>();
		queryWrapper.orderByDesc("updated");
		mpPage=itemMapper.selectPage(mpPage, queryWrapper);
		long total=mpPage.getTotal();
		List<Item> itemList=mpPage.getRecords();
		return new EasyUITable(total,itemList);
	}

	@Override
	@Transactional //开启事务控制
	public void saveItem(Item item) {
		/*Date date=new Date();
		item.setStatus(1).setCreated(date).setUpdated(date);*/
		item.setStatus(1);
		itemMapper.insert(item);
	}

	@Override
	public void updateItem(Item item) {
		itemMapper.updateById(item);
	}

	@Override
	public void deleteItems(Long... ids) {
		//方式1：利用mp(mybatis plus)方式实现
		List<Long> longList= Arrays.asList(ids);
		itemMapper.deleteBatchIds(longList);

		//方式2：手写sql完成数据的删除
	}

}
























